import { ref,  onUnmounted } from "vue";
const echartsRef = ref(null);
export const useResize = (dom) => {
    echartsRef.value = dom;
 
  /* 根据浏览器大小推断缩放比例 */
  const getScale = (width = 1920, height = 1080) => {
    let ww = window.innerWidth / width;
    let wh = window.innerHeight / height;
    return ww < wh ? ww : wh;
  };
  if(echartsRef.value){
    echartsRef.value.style.transform = `scale(${getScale()}) translate(-50%, -50%)`;
    echartsRef.value.style.height = 1080+'px'
    echartsRef.value.style.width = 1920+'px'
  }
  const resize = () => {
    if (echartsRef.value) {
      // scale 縮放：如果屏幕窗口發生變化的時候 然后调用getScale方法计算出要缩放的比例 进行缩放
      // translate 移动 但相对于自身 又移动了 -50% 相当于位置没有改变
      echartsRef.value.style.transform = `scale(${getScale()}) translate(-50%, -50%)`;
    }
  };
  window.addEventListener("resize", resize);

  onUnmounted(() => {
    window.removeEventListener("resize", resize);
  });

};
